package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.wms.dao.InterfaceSapDetailDao;
import com.cxyd.modules.wms.dto.InterfaceSapDTO;
import com.cxyd.modules.wms.dto.InterfaceSapDetailDTO;
import com.cxyd.modules.wms.entity.InterfaceSapDetailEntity;
import com.cxyd.modules.wms.service.InterfaceSapDetailService;
import com.cxyd.modules.wms.service.InterfaceSapService;
import com.cxyd.modules.wms.service.PurchaseDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InterfaceSapDetailServiceImpl extends CrudServiceImpl<InterfaceSapDetailDao, InterfaceSapDetailEntity, InterfaceSapDetailDTO> implements InterfaceSapDetailService {
    @Autowired
    private InterfaceSapService interfaceSapService;
    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Override
    public QueryWrapper<InterfaceSapDetailEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public PageData<InterfaceSapDetailDTO> dnPage(Map<String, Object> params) {
        //判断DN是否为空
        final String deliverNo = (String) params.get("deliverNo");
        if (StringUtils.isBlank(deliverNo)) {
            return null;
        }
        final InterfaceSapDTO dto = interfaceSapService.selectByDeliverNo(deliverNo);
        if (dto == null) {
            return null;
        }
        final QueryWrapper<InterfaceSapDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("deliver_no", deliverNo);
        final IPage<InterfaceSapDetailEntity> page = baseDao.selectPage(getPage(params, "id", true),
                wrapper);
        return getPageData(page, InterfaceSapDetailDTO.class);
    }

    @Override
    public List<InterfaceSapDetailDTO> dnList(Map<String, Object> params) {
        //判断DN是否为空
        final String deliverNo = (String) params.get("deliverNo");
        final QueryWrapper<InterfaceSapDetailEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("deliver_no", deliverNo);
        final List<InterfaceSapDetailEntity> list = baseDao.selectList(wrapper);
        return ConvertUtils.sourceToTarget(list, InterfaceSapDetailDTO.class);
    }
}
